package com.xbk.seckill.common.exception;

import com.alibaba.csp.sentinel.slots.block.authority.AuthorityException;
import com.alibaba.csp.sentinel.slots.block.degrade.DegradeException;
import com.alibaba.csp.sentinel.slots.block.flow.FlowException;
import com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowException;
import com.alibaba.csp.sentinel.slots.system.SystemBlockException;
import com.xbk.seckill.common.response.ResponseMessage;
import com.xbk.seckill.common.response.ResponseMessageBuilder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.lang.reflect.UndeclaredThrowableException;

/**
 * 全局统一异常处理器
 *
 * @author xiexu
 * @date 2025/1/11 16:17
 */
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {

    /**
     * 全局异常处理，统一返回状态码
     */
    @ExceptionHandler(SeckillException.class)
    public ResponseMessage<String> handleSeckillException(SeckillException e) {
        log.error("服务器抛出了异常：{}", e);
        return ResponseMessageBuilder.build(e.getCode(), e.getMessage());
    }

    /**
     * Sentinel异常处理
     */
    @ExceptionHandler(UndeclaredThrowableException.class)
    public ResponseMessage<String> handleUndeclaredThrowableException(UndeclaredThrowableException e) {
        ErrorCode errorCode = null;
        if (e.getUndeclaredThrowable() instanceof FlowException) {
            errorCode = ErrorCode.SENTINEL_FLOW;
        } else if (e.getUndeclaredThrowable() instanceof DegradeException) {
            errorCode = ErrorCode.SENTINEL_DEGRADE;
        } else if (e.getUndeclaredThrowable() instanceof ParamFlowException) {
            errorCode = ErrorCode.SENTINEL_PARAMAS;
        } else if (e.getUndeclaredThrowable() instanceof SystemBlockException) {
            errorCode = ErrorCode.SENTINEL_SYSTEM;
        } else if (e.getUndeclaredThrowable() instanceof AuthorityException) {
            errorCode = ErrorCode.SENTINEL_AUTHORITY;
        } else {
            errorCode = ErrorCode.SENTINEL_FLOW;
        }
        return ResponseMessageBuilder.build(errorCode.getCode(), errorCode.getMesaage());
    }

    /**
     * 全局异常处理，统一返回状态码
     */
    @ExceptionHandler(Exception.class)
    public ResponseMessage<String> handleException(Exception e) {
        log.error("服务器抛出了异常：{}", e);
        return ResponseMessageBuilder.build(ErrorCode.SERVER_EXCEPTION.getCode(), e.getMessage());
    }

}
